#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#include<ext/rope>
#define rep(i,l,r) for(int i=(l),i##end=(r);i<=i##end;++i)
#define per(i,r,l) for(int i=(r),i##end=(l);i>=i##end;--i)
#define ll long long
#define int ll
#define double long double
#define pii pair<int,int>
#define fi first
#define se second
#define pb push_back
#define popcnt __builtin_popcount
#define rbtree(way) tree<way,null_type,less<way>,rb_tree_tag,tree_order_statistics_node_update>
using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;
const int maxn=1e6+10,maxm=1e6+10,mod=998244353,inf=1e18;
inline int ksm(int x,int k,int mod=mod){
	int ans=1;
	for(x%=mod;k;k>>=1,x=x*x%mod) if(k&1) ans=ans*x%mod;
	return ans;
}

int n,m,ans=inf,a[maxn];

namespace solve{

int sg[maxn<<2],lz[maxn<<2];
void change(int id,int l,int r,int lq,int rq,int x){
	if(lq<=l&&r<=rq){
		sg[id]+=x,lz[id]+=x;
		return;
	}
	sg[id<<1]+=lz[id],lz[id<<1]+=lz[id],sg[id<<1|1]+=lz[id],lz[id<<1|1]+=lz[id],lz[id]=0;
	int mid=l+r>>1;
	if(lq<=mid) change(id<<1,l,mid,lq,rq,x);
	if(rq>mid) change(id<<1|1,mid+1,r,lq,rq,x);
	sg[id]=min(sg[id<<1],sg[id<<1|1]);
}
int query(int id,int l,int r,int x){
	if(l==r) return sg[id];
	sg[id<<1]+=lz[id],lz[id<<1]+=lz[id],sg[id<<1|1]+=lz[id],lz[id<<1|1]+=lz[id],lz[id]=0;
	int mid=l+r>>1;
	if(x<=mid) return query(id<<1,l,mid,x);
	return query(id<<1|1,mid+1,r,x);
}
int find(int id,int l,int r,int x){
	if(l==r) return l;
	sg[id<<1]+=lz[id],lz[id<<1]+=lz[id],sg[id<<1|1]+=lz[id],lz[id<<1|1]+=lz[id],lz[id]=0;
	int mid=l+r>>1;
	if(x<sg[id<<1|1]) return find(id<<1,l,mid,x);
	return find(id<<1|1,mid+1,r,x); 
}

int p1[maxn],p2[maxn];
pii a1[maxn];

int mn[maxn<<2];
void modify(int id,int l,int r,int p,int x){
	if(l==r){
		mn[id]=x;
		return;
	}
	int mid=l+r>>1;
	if(p<=mid) modify(id<<1,l,mid,p,x);
	else modify(id<<1|1,mid+1,r,p,x);
	mn[id]=min(mn[id<<1],mn[id<<1|1]);
}
int querymn(int id,int l,int r,int lq,int rq){
	if(lq<=l&&r<=rq) return mn[id];
	int mid=l+r>>1,ans=inf;
	if(lq<=mid) ans=min(ans,querymn(id<<1,l,mid,lq,rq));
	if(rq>mid) ans=min(ans,querymn(id<<1|1,mid+1,r,lq,rq));
	return ans;
}

signed main(){
	rep(i,1,n<<2) sg[i]=lz[i]=p1[i]=p2[i]=0,mn[i]=inf;
	rep(i,1,n) a1[i]={a[i],n-i};
	sort(a1+1,a1+n+1);
	rep(i,1,n) if(a1[i].fi==a1[i+1].fi&&m<=i+1) ans=min(ans,2*a1[i].fi);
	rep(i,1,n){
		int x=n-a1[i].se,vl=query(1,1,n,x),cnt1=lower_bound(a1+1,a1+n+1,(pii){a[x],n})-a1-1;
		change(1,1,n,x,n,1);
		if(i-vl>=m) p1[x]=find(1,1,n,vl+m-1);
		if(cnt1>=m-1) p2[x]=find(1,1,n,min(i,vl+cnt1-m+2));
//		cout<<a[x]<<":"<<x<<"]\n";
//		rep(i,1,n) cout<<query(1,1,n,i)<<"|";cout<<"\n";
	}
	per(i,n,1){
		int x=n-a1[i].se;
//		rep(x,1,n) rep(y,x,n) cout<<querymn(1,1,n,x,y)<<",\n"[y==yend];cout<<"\n";
		modify(1,1,n,x,a[x]);
		if(p1[x]) ans=min(ans,querymn(1,1,n,p1[x],n)+a[x]);
		if(x<p2[x]) ans=min(ans,querymn(1,1,n,x+1,p2[x])+a[x]);
	}
//	rep(i,1,n) cout<<p1[i]<<",\n"[i==iend];
//	rep(i,1,n) cout<<p2[i]<<",\n"[i==iend];
	return 0;
}
}

signed main(){
	freopen("sub.in","r",stdin),freopen("sub.out","w",stdout);
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	cin>>n>>m;
	rep(i,1,n) cin>>a[i];
	int ans1=inf;
	rep(l,1,n) rep(r,l+1,n){
		int cnt0=0,cnt1=0;
		rep(i,l+1,r-1) cnt0+=a[i]<=min(a[l],a[r]);
		rep(i,1,l-1) cnt1+=a[i]<=min(a[l],a[r]);
		rep(i,r+1,n) cnt1+=a[i]<=min(a[l],a[r]);
		if((cnt0>=m-2||cnt1>=m-2)&&a[l]+a[r]<ans1) ans1=a[l]+a[r],cout<<l<<","<<r<<":"<<ans1<<"]\n";
	}
	solve::main();
	rep(i,1,n>>1) swap(a[i],a[n-i+1]);
	solve::main(),cout<<ans;
	return 0;
}
